package com.ps.service;

import java.io.Serializable;
import java.util.List;

import javax.servlet.http.HttpServletRequest;


import org.apache.log4j.Logger;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;

import com.ps.dao.BaseDaoSupport;
import com.ps.entity.Result;
import com.ps.entity.Status;
import com.ps.util.Kit;
import com.ps.util.PaginationSupport;
import com.ps.util.R;


public abstract class BaseServiceSupport<T> {

	protected final Logger log = Logger.getLogger(getClass());

	//protected BaseDaoSupport<T> baseDao;

	public abstract BaseDaoSupport<T> getBaseDao(); 



	public boolean exists(Integer id) {
		return getBaseDao().exists(id);
	}

	public Result findById(Integer id) {
		Result result = Kit.createResult();
		try {
			T o = getBaseDao().findById(id);
			
			if (o == null) {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			} else {
				result.setDatas(o);
			}
		} catch (Exception e) {
			log.error("findById exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
		}
		return result;
	}
	
	public Result findAll() {
		Result result = Kit.createResult();
		try {
			List<T> o = getBaseDao().findAll();
			if (o != null && o.size() > 0) {
				result.setDatas(o);
			} else {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			}
			
		} catch (Exception e) {
			log.error("findAll exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
		}
		
		return result;
	}



	public Result save(T object) {
		Result result = Kit.createResult();
		try {
			Integer o = getBaseDao().save(object);
			result.setDatas(o);
			result.getStatus().setMessage(R.string.getProperty("addsucces"));
		} catch (Exception e) {
			log.error("save exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.setDatas(object);
			result.getStatus().setMessage(R.string.getProperty("addfail"));
		}
		return result;
	}

	public Result update(T object) {
		Result result = Kit.createResult();
		try {
			getBaseDao().update(object);
			result.getStatus().setMessage(R.string.getProperty("updsucces"));
		} catch (Exception e) {
			log.error("update exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.setDatas(object);
			result.getStatus().setMessage(R.string.getProperty("updfail"));
		}
		return result;
	}

	public void flush() {
		getBaseDao().flush();
	}

	public Result delete(T object) {
		Result result = Kit.createResult();
		try {
			getBaseDao().delete(object);
			result.getStatus().setMessage(R.string.getProperty("delsucces"));
		} catch (Exception e) {
			log.error("delete exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("delfail"));
		}
		return result;
		
	}
	
	public Result delete(Integer id) {
		Result result = Kit.createResult();
		try {
			getBaseDao().delete(id);
			result.getStatus().setMessage(R.string.getProperty("delsucces"));
			result.setDatas(id);
		} catch (Exception e) {
			log.error("delete exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("delfail"));
		}
		return result;

	}
	
	public Result findByCriteria(final DetachedCriteria detachedCriteria ){
		Result result = Kit.createResult();
		try {
			List<T> o = getBaseDao().findByCriteria(detachedCriteria);
			if (o != null && o.size() > 0) {
				result.setDatas(o);
			} else {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			}
		} catch (Exception e) {
			log.error("findByCriteria exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
		}
		return result;
		
		
	}
	
	public Result findPageByCriteria(final DetachedCriteria detachedCriteria, final int pageSize,final int startIndex,final List<Order> orderList){
		Result result = Kit.createResult();
		try {
			PaginationSupport<T> o = getBaseDao().findPageByCriteria(detachedCriteria,pageSize,startIndex,orderList);
			if (o != null && o.getItems() != null && o.getItems().size() > 0) {
				result.setDatas(o);
			} else {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			}
		} catch (Exception e) {
			log.error("findPageByCriteria exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
		}
		return result;

	}
	
	public Result findPageByCriteria(final DetachedCriteria detachedCriteria,
			final int pageSize,final int startIndex,final Order order){
		Result result = Kit.createResult();
		try {
			PaginationSupport<T> o = getBaseDao().findPageByCriteria(detachedCriteria,pageSize,startIndex,order);
			if (o != null && o.getItems() != null && o.getItems().size() > 0) {
				result.setDatas(o);
			} else {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			}
		} catch (Exception e) {
			log.error("findPageByCriteria exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
		}
		return result;
	}
	
	public Result saveOrUpdate(final T object){
		Result result = Kit.createResult();
		try {
			this.getBaseDao().saveOrUpdate(object);
			result.getStatus().setMessage(R.string.getProperty("updsucces"));
		} catch (Exception e) {
			log.error("saveOrUpdate exception ", e);
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("updfail"));
		}
		return result;
	}
	
	public Result findByProperty(String propertyName, Object value) {
		Result result = Kit.createResult();
		try {
			List<T> o = this.getBaseDao().findByProperty(propertyName, value);
			if (o != null && o.size() > 0) {
				result.setDatas(o);
			} else {
				result.getStatus().setCode(Status.EMPTY);
				result.getStatus().setMessage(R.string.getProperty("queryempty"));
			}
		} catch (Exception e) {
			result.getStatus().setCode(Status.FAIL);
			result.getStatus().setMessage(R.string.getProperty("queryfail"));
			log.error("findByProperty exception ", e);
		}
		
		return result;
	}
	
	public int getCountByProperty(String propertyName) {
		return this.getBaseDao().getCountByProperty(propertyName);
	}
	
	public int getCountByProperty(String propertyName, Object value) {
		return this.getBaseDao().getCountByProperty(propertyName, value);
	}
	
}

